import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaTools, WeaErrorPage, WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools;
import EHeader from '../common/EHeader';
import CooperationCom from './cooperation';
import FooterMore from "../common/FooterMore";
import { NoRightCom, handleHeight } from '../../util/common';
//元素组件
class Cooperation extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            edata: null,
            refresh: false,
        }
        this.handleEData = this.handleEData.bind(this);
        this.handleRefresh = this.handleRefresh.bind(this);
        this.handleDelete = this.handleDelete.bind(this);
        this.onRef = this.onRef.bind(this);
        this.handleTabData = this.handleTabData.bind(this);

        if(window.element_cooperation_refresh && typeof window == 'object'){
            window.element_cooperation_refresh[props.config.params.eid] = this.handleRefresh.bind(this);
        }else{
            let obj = {};
            obj[props.config.params.eid] = this.handleRefresh.bind(this);
            window.element_cooperation_refresh = obj
        }
    }
    handleDelete(){
        this.setState({
            edata: null,
        });
        ls.set('edata-'+this.props.config.params.eid, null);
    }
    handleRefresh(){
        this.setState({
            refresh: true,
        });
        this.handleEData(this.props.config.params);
    }
    handleEData(params){
        const { eid, api } = params;
        WeaTools.callApi(api || '/api/portal/element/cooperation', 'POST', {...params}).then(result=> {
            ls.set('edata-'+eid, result);
            this.setState({
                edata: result,
                refresh: false,
            });
       });
    }
    componentDidMount(){
        const { config = {} } = this.props;
        const { params = {} } = config;
        const { eid } = params;
        const edata = ls.getJSONObj(`edata-${eid}`);
        if (config.islsdata && edata) {
            this.setState({ edata });
        } else {
            this.handleEData(params);
        }
    }
    componentWillReceiveProps(nextProps){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = { 
            item: nextProps.config.item, 
            params: nextProps.config.params, 
            isHasRight: nextProps.config.isHasRight,
        };
        if(!isEqual(config, _config) || nextProps.config.isRender){
            this.handleEData(nextProps.config.params);
        }
    }
    shouldComponentUpdate(nextProps, nextState){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = {
            item: nextProps.config.item,
            params: nextProps.config.params,
            isHasRight: nextProps.config.isHasRight,
        };
        return !isEqual(this.state, nextState) || !isEqual(config, _config) || !isEmpty(nextProps.fontSizeMp)
    }
    onRef(ref){
        this.child = ref;
    }
    handleTabData(tabid){
        this.child.handleTabData(tabid)
    }
    render() {
        const { edata={}, refresh } = this.state;
        const { config, fontSizeMp } = this.props;
        if(!config || !edata) return <div/>;
        const { currenttab='1', tabids, titles, esetting={} } = edata;
        const { isHasRight, item } = config;
        const { eid, ebaseid, content, header, contentview, style } = item;
        const { style: style1= {} } = contentview;
        const datas = {
            currentTab: currenttab,
            tabids,
            titles,
            handleTabData: this.handleTabData,
            tabwidth: esetting.tabwidth,
            methodvalue: esetting.methodvalue,
            isAlwaysShow: esetting.isAlwaysShow,
            tabstyle: esetting.tabstyle,
            tabvalue: esetting.tabvalue,
        }
        let EContentHtml = <div/>;
        // window.elements_more_params[config.params.eid] = {isRouteUrl: true, tabid: edata.currenttab || '1'};
        if(isHasRight){
            if(!isEmpty(edata)) EContentHtml = <CooperationCom ecId={`${this && this.props && this.props.ecId || ''}_CooperationCom@kqukh2`} onRef={this.onRef} config={config} fontSizeMp={fontSizeMp} handleDelete={this.handleDelete} handleRefresh={this.handleRefresh} edata={edata}/>;
        }else{
            EContentHtml = <NoRightCom ecId={`${this && this.props && this.props.ecId || ''}_NoRightCom@dvb9as`}/>
        }
        return <div className="item"
                    style={style}
                    id={`item_${eid}`} 
                    data-eid={eid} data-ebaseid={ebaseid} 
                    data-needRefresh={item.needRefresh} 
                    data-cornerTop={item.cornerTop} 
                    data-cornerTopRadian={item.cornerTopRadian}
                    data-cornerBottom={item.cornerBottom} 
                    data-cornerBottomRadian={item.cornerBottomRadian}>
                    <EHeader ecId={`${this && this.props && this.props.ecId || ''}_EHeader@yf09iw`} datas={datas} config={config} handleRefresh={this.handleRefresh} handleDelete={this.handleDelete} fontSizeMp={fontSizeMp}/>
                    <div className="setting" id={`setting_${eid}`}></div>
                    <div className="content" id={`content_${eid}`} style={{width:'auto',_width:'100%'}}>
                        <div className="content_view" id={`content_view_id_${eid}`} style={{height: style1.height}}>
                            <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@5ro6he`} spinning={refresh}>{EContentHtml}</Spin>
                        </div>
                        <div style = {{textAlign:'right'}} id={`footer_${eid}`}>
                            <FooterMore ecId={`${this && this.props && this.props.ecId || ''}_FooterMore@fet4sq`} config={this.props.config}/>
                        </div>
                    </div>
            </div>;
    }
}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@7fom1i`} msg = { hasErrorMsg ? this.props.error : getLabel(385218,"对不起，该页面异常，请联系管理员！") }/>
        );
    }
}
Cooperation = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(Cooperation);
export default Cooperation;



